(function($, doc) {    
	var $bubble, 
        icon = '<span class="bubbleIcon {0}"></span>', 
        timeoutPrt = null, 
        hideSpeed = 600, 
        bubbleObjDelay = 5000,
         
        //初始化或重置bubble对象.
        initBubbleObj = function() {
            var _this = this;
    		if(timeoutPrt) {
    			clearTimeout(timeoutPrt);
    		}
    		if(!$bubble){
    		    $bubble = $([
                   '<div class="bubble" style="display:none;">', 
                       '<div class="bubbleText"></div>', 
                       '<div class="bubbleToolbar confirm">', 
                           '<button class="yes">OK</button>', 
                           '<button class="no">Cancel</button>', 
                       '</div>',
                       '<div class="bubbleToolbar alert">',
                           '<button class="close">Close</button>', 
                       '</div>',
                   '</div>'
                ].join(''))
                //若网页处于全屏状态，则将bubble元素添加到全屏元素中; 非全屏时添加到document.body中.
                .appendTo( ujs.browser.fullScreenApi.fullScreenElement ? ujs.browser.fullScreenApi.fullScreenElement : doc.body);
                
                $bubble.delegate('.bubbleToolbar > button', 'click', function(){
                    var $button = $(this); 
                    if($button.hasClass('close')){
                        _this.hide();
                    }
                });              
    		}
    		$bubble.find('.bubbleToolbar.confirm').hide();
    		$bubble.css('top', -$bubble.outerHeight()).hide().find('.bubbleText').html('');
    	};
    	
	//气泡提示.
	var bubble = {
	    /**
	     * 生成一条bubble消息.
         * @param {Object} text : 消息文本.
         * @param {Object} type : 消息类型，info/error/confirm/warning.
         * @param {Object} detail : 消息详细文本.
         * @param {Object} autoHide : 是否自动隐藏，默认是true.
	     */
		msg : function(text, type, detail, autoHide) {
			var _this = this;
			text = String(text).safeString();
			type = type || 'info';
			autoHide = ( autoHide !== false );
			
			initBubbleObj.call(this);
			
			$bubble.show().find('.bubbleText').html(
			    icon.format(type) + text + ( 
			        detail ? 
			        [
			             "<a class='showDetail'>error detail</a>",
			             "<div class='bubbleDetail'>{0}</div>"
		            ].join('').format(detail) : 
			        ""
		        )
	        ).addClass(type);
			
			//详细说明信息.
			if(detail) {
				$bubble.find('.showDetail').click(function(e) {
					if(timeoutPrt) {
						clearTimeout(timeoutPrt);
					}
					$bubble.find('.bubbleDetail').show('fast');
					e.preventDefault();
				});				
			}
			
			//关闭按钮.
			if(detail || autoHide === false){
			    $bubble.find('.bubbleToolbar.alert').show();
			} else {
			    $bubble.find('.bubbleToolbar.alert').hide();
			}
						
			//隐藏工具条.
			$bubble.find('.bubbleToolbar.confirm').hide();
						
			$bubble.css('left', ( $(window).width() - $bubble.outerWidth() ) / 2);
			
			//自动隐藏.			
			$bubble.animate({
				top : 0
			}, hideSpeed, function() {
			    if(autoHide && !detail){
    				timeoutPrt = (function() {
    					$bubble.animate({
    						top : -$bubble.outerHeight()
    					}, hideSpeed / 4, function(){
    					    bubble.hide();
    					});
    				}).defer(bubbleObjDelay);
				}
			});			
		},
		//显示一个提示框，但并不自动隐藏.
		alert : function(text, type){
		    this.msg(text, type, null, false);
		},
		hide : function() {
			if(timeoutPrt) {
				clearTimeout(timeoutPrt);
			}
			if($bubble){
    			$bubble.animate({
    				top : -$bubble.outerHeight()
    			}, hideSpeed / 4, function() {
    				$bubble.find('.bubbleObjDetail').remove();
    				$bubble.find('.bubbleToolbar.alert').hide();
    			});
			}
		},
		/**
		 * 提示框, yes/no.
         * @param {Object} text
         * @param {Object} yesHandler
         * @param {Object} noHandler
		 */
		confirm : function(text, yesHandler, noHandler) {
			var _this = this;
			text = text.safeString();
			
			initBubbleObj.call(this);
			
			$bubble.find('.bubbleToolbar.confirm').show();
			$bubble.find('.bubbleToolbar.alert').hide();
			$bubble.show().find('.bubbleText').html(icon.format('confirm') + text);
			
			$bubble.css('left', ( $(window).width() - $bubble.outerWidth() ) / 2);
			$bubble.css('top', -$bubble.outerHeight());
			$bubble.animate({
				top : 0
			}, hideSpeed);
			$bubble.find('button.yes').unbind('click').bind('click', function() {
				_this.hide();
				yesHandler && yesHandler();
			}).focus();
			$bubble.find('button.no').unbind('click').bind('click', function() {
				_this.hide();
				noHandler && noHandler();
			});
		},
		/**
         * 提示框, yes/no.
         * @param {Object} text
         * @param {Object} yesHandler
         * @param {Object} noHandler
         */
        prompt : function(tip, initText, yesHandler, noHandler) {
            var _this = this;
            tip = tip.safeString();
                        
            initBubbleObj.call(this);
            
            $bubble.find('.bubbleToolbar.confirm').show();
            $bubble.find('.bubbleToolbar.alert').hide();
            
            $bubble.show().find('.bubbleText').html(
                icon.format('prompt') + tip + 
                '<div class="promptBoxWrapper"><input class="promptBox" value="{0}" /></div>'.format(initText)
            );
            
            $bubble.css('left', ( $(window).width() - $bubble.outerWidth() ) / 2);
            $bubble.css('top', -$bubble.outerHeight());
            $bubble.animate({
                top : 0
            }, hideSpeed);
            
            $bubble.find('button.yes').unbind('click').bind('click', function() {
                _this.hide();
                yesHandler && yesHandler($bubble.find('.bubbleText input').val());
            }).focus();
            $bubble.find('button.no').unbind('click').bind('click', function() {
                _this.hide();
                noHandler && noHandler();
            });
        }, 
		/**
		 * 设置提示气泡在那个窗口内显示，默认为document.body. 
         * @param {Object} container : DOM/jQueryObj
		 */
		setContainer : function(container){		    	    
		    $('.bubble').appendTo( this.container = $( container || doc.body ) );
		}
	};

    //---------------------------------------------------------------------------------------------
	
	//TODO 老版本兼容性代码，新项目中去掉.
	window.bubble = window.top.bubble || bubble;
	
	//---------------------------------------------------------------------------------------------
	
	ujs.define('bubble', 'ujs.ui', function(){
	    //若当前页面在iframe内时，使用顶级页面的bubble对象.
	    if(window.top.ujs && window.top.ujs.ui && window.top.ujs.ui.bubble){
	        return window.top.ujs.ui.bubble
	    } else {
            return bubble;
        }
    });    
	
})($, document);